package com.video.window;

import com.video.dao.VideoManager;
import com.video.utils.constant.Constants;
import com.video.utils.StringUtils;
import com.video.utils.ViewUtils;
import org.apache.commons.io.FilenameUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

public class MainVideoGPTWindow implements ActionListener {

    private JTextField fieldVideoPath;
    private JTextField fieldVideoSrc;
    private JTextField fieldVideoInfo;
    private JTextArea logTextArea;
    private JButton btnCreate;

    public MainVideoGPTWindow() {
        JFrame frame = new JFrame();
        frame.setVisible(true);
        frame.setBounds(100, 100, 450, 450);
        frame.setMinimumSize(new Dimension(450, 450));
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setTitle("视频解说");
        JLabel labelVideoPath = new JLabel("文件位置");
        fieldVideoPath = new JTextField();
        ViewUtils.enableDragAndDrop(fieldVideoPath);
        JLabel labelVideoSrc = new JLabel("视频字幕");
        fieldVideoSrc = new JTextField();
        ViewUtils.enableDragAndDrop(fieldVideoSrc);
        JLabel labelVideoInfo = new JLabel("视频简介");
        fieldVideoInfo = new JTextField();
        ViewUtils.enableDragAndDrop(fieldVideoInfo);
        logTextArea = new JTextArea();
        logTextArea.setEditable(false);
        logTextArea.setLineWrap(true);
        logTextArea.setWrapStyleWord(true);
        JScrollPane logScrollPane = new JScrollPane(logTextArea);

        btnCreate = new JButton("生成视频解说");
        btnCreate.addActionListener(this);
        GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
        groupLayout.setHorizontalGroup(
                groupLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                        .addGroup(
                                groupLayout.createSequentialGroup()
                                        .addGroup(
                                                groupLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                                        .addGroup(
                                                                GroupLayout.Alignment.LEADING, groupLayout.createSequentialGroup()
                                                                        .addGap(19)
                                                                        .addComponent(labelVideoPath)
                                                                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                                                        .addComponent(
                                                                                fieldVideoPath,
                                                                                Constants.COMPONENT_MIN,
                                                                                Constants.COMPONENT_PREF,
                                                                                Short.MAX_VALUE
                                                                        )
                                                        )
                                                        .addGroup(
                                                                GroupLayout.Alignment.LEADING, groupLayout.createSequentialGroup()
                                                                        .addGap(19)
                                                                        .addComponent(labelVideoSrc)
                                                                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                                                        .addComponent(
                                                                                fieldVideoSrc,
                                                                                Constants.COMPONENT_MIN,
                                                                                Constants.COMPONENT_PREF,
                                                                                Short.MAX_VALUE
                                                                        )
                                                        )
                                                        .addGroup(
                                                                GroupLayout.Alignment.LEADING, groupLayout.createSequentialGroup()
                                                                        .addGap(19)
                                                                        .addComponent(labelVideoInfo)
                                                                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                                                        .addComponent(
                                                                                fieldVideoInfo,
                                                                                Constants.COMPONENT_MIN,
                                                                                Constants.COMPONENT_PREF,
                                                                                Short.MAX_VALUE
                                                                        )
                                                        )
                                                        .addGroup(
                                                                GroupLayout.Alignment.LEADING, groupLayout.createSequentialGroup()
                                                                        .addGap(19)
                                                                        .addComponent(
                                                                                logScrollPane,
                                                                                Constants.COMPONENT_MIN,
                                                                                Constants.COMPONENT_PREF,
                                                                                Short.MAX_VALUE
                                                                        )
                                                        )
                                                        .addGroup(
                                                                GroupLayout.Alignment.TRAILING, groupLayout.createSequentialGroup().addContainerGap()
                                                                        .addComponent(btnCreate, GroupLayout.DEFAULT_SIZE, 438, Short.MAX_VALUE)
                                                        )
                                        )
                                        .addContainerGap()
                        )
        );
        groupLayout.setVerticalGroup(
                groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addGroup(
                                groupLayout.createSequentialGroup()
                                        .addContainerGap()
                                        .addGroup(
                                                groupLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                                        .addComponent(
                                                                fieldVideoPath,
                                                                GroupLayout.PREFERRED_SIZE,
                                                                GroupLayout.DEFAULT_SIZE,
                                                                GroupLayout.PREFERRED_SIZE
                                                        )
                                                        .addComponent(labelVideoPath)
                                        )
                                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(
                                                groupLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                                        .addComponent(
                                                                fieldVideoSrc,
                                                                GroupLayout.PREFERRED_SIZE,
                                                                GroupLayout.DEFAULT_SIZE,
                                                                GroupLayout.PREFERRED_SIZE
                                                        )
                                                        .addComponent(labelVideoSrc)
                                        )
                                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(
                                                groupLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                                        .addComponent(
                                                                fieldVideoInfo,
                                                                GroupLayout.PREFERRED_SIZE,
                                                                GroupLayout.DEFAULT_SIZE,
                                                                GroupLayout.PREFERRED_SIZE
                                                        )
                                                        .addComponent(labelVideoInfo)
                                        )
                                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(
                                                groupLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                                        .addComponent(
                                                                logScrollPane,
                                                                GroupLayout.PREFERRED_SIZE,
                                                                GroupLayout.DEFAULT_SIZE,
                                                                Short.MAX_VALUE
                                                        )
                                        )
                                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(
                                                groupLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                                        .addComponent(
                                                                btnCreate,
                                                                GroupLayout.PREFERRED_SIZE,
                                                                GroupLayout.DEFAULT_SIZE,
                                                                GroupLayout.PREFERRED_SIZE
                                                        )
                                        )
                                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        )

        );
        frame.getContentPane().setLayout(groupLayout);
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        new Thread(() -> {
            String videoPath = fieldVideoPath.getText().toString();
            if (StringUtils.isNotEmpty(videoPath)) {
                File videoPathFile = new File(videoPath);
                if (videoPathFile.exists()) {
                    log("分享视频中的音频成功");
                    File outputPathFile = new File(videoPathFile.getParentFile(), "output");
                    if (!outputPathFile.exists()) {
                        outputPathFile.mkdirs();
                    }
                    File videoMp3File = new File(outputPathFile, FilenameUtils.getBaseName(videoPath) + ".mp3");
                    final boolean extractSuccess = VideoManager.getInstance().extractAudio(videoPath, videoMp3File.getAbsolutePath());
                    if (extractSuccess) {
                        log("音频格式转换成wav成功");
                        File videoWavFile = new File(outputPathFile, FilenameUtils.getBaseName(videoPath) + ".wav");
                        boolean wavSuccess = VideoManager.getInstance().convertMp3ToWav(videoMp3File.getAbsolutePath(), videoWavFile.getAbsolutePath());
                        if (wavSuccess) {
                            File audioTextFile = new File(outputPathFile, FilenameUtils.getBaseName(videoPath) + ".txt");
                            VideoManager.getInstance().recognize(videoWavFile.getAbsolutePath(), audioTextFile.getAbsolutePath());
//                            File musicOutputPathFile = new File(outputPathFile, FilenameUtils.getBaseName(videoPath) + "_music.wav");
//                            File dialogueOutputPathFile = new File(outputPathFile, FilenameUtils.getBaseName(videoPath) + "_dialogue.wav");
//                            boolean musicSuccess = VideoManager.getInstance().separateAudio(videoMp3File, musicOutputPathFile, dialogueOutputPathFile);
//                            if (musicSuccess) {
//                                log("分享视频中的分离台词成功");
//                            } else {
//                                log("分享视频中的分离台词失败");
//                            }
                        } else {
                            log("分享视频中的音频成功");
                        }
                    } else {
                        log("音频格式转换成wav失败");
                    }
                }
            }
        }).start();

    }

    private void log(String message) {
        System.out.println(message);
    }
}
